package com.HMI;

import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.text.SimpleDateFormat;
import java.util.Calendar;

import javax.swing.JPanel;
import javax.swing.SwingConstants;

import com.Resources.CustomColor;

@SuppressWarnings("serial")
public class PopupWindow extends AlphaPanel {

	private JPanel scheduler;
	private JPanel mainPanel;
	private MenuButton btnCancel;
	private MenuButton btnAddRemoveUpdate;
//	private MenuButton btnPrev;
//	private MenuButton btnNext;
	private PopupWindow popup;
	
	private PanelText weekday;
	private PanelText month;
	private PanelText day;
	
	private PanelText hour;
	private PanelText minute;
	private PanelText ampm;
	
	private MenuButton nextWeekday;
	private MenuButton prevWeekday;
	private MenuButton nextMonth;
	private MenuButton prevMonth;
	private MenuButton nextDate;
	private MenuButton prevDate;
	private MenuButton nextHour;
	private MenuButton prevHour;
	private MenuButton nextMinute;
	private MenuButton prevMinute;
	private MenuButton nextAmPm;
	private MenuButton prevAmPm;
	private Calendar calendar;
	private Window parent;
	private boolean shockScheduled;
	
	public PopupWindow(Window parent) {
		super(0, 0, 800, 480);
		this.parent = parent;
		this.setBackground(CustomColor.TRANSPARENT_GRAY);
		popup = this;
		this.addMouseListener(new MouseListener() {

			@Override
			public void mouseClicked(MouseEvent e) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void mousePressed(MouseEvent e) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void mouseReleased(MouseEvent e) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void mouseEntered(MouseEvent e) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void mouseExited(MouseEvent e) {
				// TODO Auto-generated method stub
				
			}
			
		});
		initializeScheduler();
	}

	private void initializeScheduler() {
		scheduler = new JPanel();
		scheduler.setBounds(184, 84, 432, 312);
		scheduler.setBackground(CustomColor.NAVY_BLUE);
		this.add(scheduler);
		scheduler.setLayout(null);
		mainPanel = new JPanel();
		mainPanel.setBounds(6, 44, 420, 224);
		mainPanel.setBackground(Color.WHITE);
		scheduler.add(mainPanel);
		mainPanel.setLayout(null);
		PanelText title = new PanelText("Schedule Shock", 0, 0, 432, 40, SwingConstants.CENTER, SwingConstants.CENTER);
		title.setForeground(Color.WHITE);
		title.setFont(new Font("DejaVu Sans", Font.PLAIN, 24));
		scheduler.add(title);
		initializeMainPanel();
		initializeButtons();
	}
	
	private void initializeMainPanel() {
	
		initializeTopRowButtons();
		initializeBottomRowButtons();
		
		weekday = new PanelText("", 25, 75,
				60, 29, SwingConstants.CENTER, SwingConstants.CENTER);
		mainPanel.add(weekday);
		
		month = new PanelText("", 85, 75,
				60, 29, SwingConstants.CENTER, SwingConstants.CENTER);
		mainPanel.add(month);
		
		day = new PanelText("", 145, 75,
				60, 29, SwingConstants.CENTER, SwingConstants.CENTER);
		mainPanel.add(day);
		
		hour = new PanelText("", 225, 75,
				60, 29, SwingConstants.CENTER, SwingConstants.CENTER);
		mainPanel.add(hour);
		
		mainPanel.add(new PanelText(":", 255, 75, 60, 29, SwingConstants.CENTER, SwingConstants.CENTER));
		
		minute = new PanelText("", 285, 75,
				60, 29, SwingConstants.CENTER, SwingConstants.CENTER);
		mainPanel.add(minute);
		
		ampm = new PanelText("", 340, 75,
				60, 29, SwingConstants.CENTER, SwingConstants.CENTER);
		mainPanel.add(ampm);
	}
	
	private void initializeTopRowButtons() {
		nextWeekday = new MenuButton("+", 30, 20, 51, 51);
		nextWeekday.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				calendar.add(Calendar.DATE, +1);
				weekday.setText(new SimpleDateFormat("EEE").format(calendar.getTime()));
				month.setText(new SimpleDateFormat("MMM").format(calendar.getTime()));
				day.setText(new SimpleDateFormat("d").format(calendar.getTime()));
			}
			
		});
		setButtonStyle(nextWeekday);
		
		nextMonth = new MenuButton("+", 91, 20, 51, 51);
		nextMonth.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				calendar.add(Calendar.MONTH, +1);
				weekday.setText(new SimpleDateFormat("EEE").format(calendar.getTime()));
				month.setText(new SimpleDateFormat("MMM").format(calendar.getTime()));
				day.setText(new SimpleDateFormat("d").format(calendar.getTime()));
			}
			
		});
		setButtonStyle(nextMonth);
		
		nextDate = new MenuButton("+", 150, 20, 51, 51);
		nextDate.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				calendar.add(Calendar.DATE, +1);
				weekday.setText(new SimpleDateFormat("EEE").format(calendar.getTime()));
				month.setText(new SimpleDateFormat("MMM").format(calendar.getTime()));
				day.setText(new SimpleDateFormat("d").format(calendar.getTime()));
			}
			
		});
		setButtonStyle(nextDate);

		nextHour = new MenuButton("+", 230, 20, 51, 51);
		nextHour.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				int hourInt = Integer.parseInt(hour.getText());
				hourInt++;
				if(hourInt == 13) {
					hourInt = 1;
				}
				hour.setText("" + hourInt);
			}
			
		});
		setButtonStyle(nextHour);

		nextMinute = new MenuButton("+", 288, 20, 51, 51);
		nextMinute.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				switch(minute.getText()) {
				case "00":
					minute.setText("15");
					break;
				case "15":
					minute.setText("30");
					break;
				case "30":
					minute.setText("45");
					break;
				case "45":
					minute.setText("00");
					break;
				}
			}
			
		});
		setButtonStyle(nextMinute);

		nextAmPm = new MenuButton("+", 346, 20, 51, 51);
		nextAmPm.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				toggleAmPm();
			}
			
		});
		setButtonStyle(nextAmPm);
	}
	
	private void initializeBottomRowButtons() {
		prevWeekday = new MenuButton("-", 30, 110, 51, 51);
		prevWeekday.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				calendar.add(Calendar.DATE, -1);
				weekday.setText(new SimpleDateFormat("EEE").format(calendar.getTime()));
				month.setText(new SimpleDateFormat("MMM").format(calendar.getTime()));
				day.setText(new SimpleDateFormat("d").format(calendar.getTime()));
			}
			
		});
		setButtonStyle(prevWeekday);
		
		prevMonth = new MenuButton("-", 91, 110, 51, 51);
		prevMonth.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				calendar.add(Calendar.MONTH, -1);
				weekday.setText(new SimpleDateFormat("EEE").format(calendar.getTime()));
				month.setText(new SimpleDateFormat("MMM").format(calendar.getTime()));
				day.setText(new SimpleDateFormat("d").format(calendar.getTime()));
			}
			
		});
		setButtonStyle(prevMonth);
		
		prevDate = new MenuButton("-", 150, 110, 51, 51);
		prevDate.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				calendar.add(Calendar.DATE, -1);
				weekday.setText(new SimpleDateFormat("EEE").format(calendar.getTime()));
				month.setText(new SimpleDateFormat("MMM").format(calendar.getTime()));
				day.setText(new SimpleDateFormat("d").format(calendar.getTime()));
			}
			
		});
		setButtonStyle(prevDate);

		prevHour = new MenuButton("-", 230, 110, 51, 51);
		prevHour.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				int hourInt = Integer.parseInt(hour.getText());
				hourInt--;
				if(hourInt == 0) {
					hourInt = 12;
				}
				hour.setText("" + hourInt);
			}
			
		});
		setButtonStyle(prevHour);

		prevMinute = new MenuButton("-", 288, 110, 51, 51);
		prevMinute.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				switch(minute.getText()) {
				case "00":
					minute.setText("45");
					break;
				case "15":
					minute.setText("00");
					break;
				case "30":
					minute.setText("15");
					break;
				case "45":
					minute.setText("30");
					break;
				}
			}
			
		});
		setButtonStyle(prevMinute);

		prevAmPm = new MenuButton("-", 346, 110, 51, 51);
		prevAmPm.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				toggleAmPm();
			}
			
		});
		setButtonStyle(prevAmPm);
	}
	
	public void refresh() {
		if(this.isVisible()) {
			this.repaint();
		}
	}
	
	public void initializeButtons() {
		btnCancel = new MenuButton("Cancel", 219, 274, 207, 32);
		btnCancel.setHorizontalAlignment(SwingConstants.CENTER);
		btnCancel.setOpaque(true);
		btnCancel.setBackground(CustomColor.BLUE);
		btnCancel.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				popup.setVisible(false);
			}
			
		});
		scheduler.add(btnCancel);
		
		btnAddRemoveUpdate = new MenuButton("Add", 6, 274, 207, 32);
		btnAddRemoveUpdate.setHorizontalAlignment(SwingConstants.CENTER);
		btnAddRemoveUpdate.setOpaque(true);
		btnAddRemoveUpdate.setBackground(CustomColor.BLUE);
		btnAddRemoveUpdate.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				int shockHour = Integer.parseInt(hour.getText());
				if(ampm.getText().equals("PM") && shockHour < 12) {
					shockHour += 12;
				} else if(ampm.getText() == "AM" && shockHour == 12) {
					shockHour = 0;
				}
				calendar.set(calendar.get(Calendar.YEAR),
						calendar.get(Calendar.MONTH),
						calendar.get(Calendar.DAY_OF_MONTH),
						shockHour,
						Integer.parseInt(minute.getText()));
				if(shockScheduled){
					parent.removeShockEvent((Calendar)calendar.clone());
					popup.setVisible(false);
				} else {
					parent.addShockEvent((Calendar)calendar.clone());
					popup.setVisible(false);
				}
			}
			
		});
		scheduler.add(btnAddRemoveUpdate);
		
//		btnPrev = new MenuButton("Prev", 6, 6, 80, 32);
//		btnPrev.setHorizontalAlignment(SwingConstants.CENTER);
//		btnPrev.setOpaque(true);
//		btnPrev.setBackground(CustomColor.BLUE);
//		btnPrev.addActionListener(new ActionListener() {
//
//			@Override
//			public void actionPerformed(ActionEvent e) {
//			}
//			
//		});
//		scheduler.add(btnPrev);
//		
//		btnNext = new MenuButton("Next", 346, 6, 80, 32);
//		btnNext.setHorizontalAlignment(SwingConstants.CENTER);
//		btnNext.setOpaque(true);
//		btnNext.setBackground(CustomColor.BLUE);
//		btnNext.addActionListener(new ActionListener() {
//
//			@Override
//			public void actionPerformed(ActionEvent e) {
//			}
//			
//		});
//		scheduler.add(btnNext);
	}

	private void setButtonStyle(MenuButton button) {
		button.setHorizontalAlignment(SwingConstants.CENTER);
		button.setOpaque(true);
		button.setBackground(CustomColor.NAVY_BLUE);
		mainPanel.add(button);
	}
	
	public void setInitialShockValues(String weekday, String month, String day, String hour, String minute, String ampm, Calendar calendar, boolean shockScheduled) {
		this.calendar = (Calendar) calendar.clone();
		
		this.weekday.setText(weekday);
		this.month.setText(month);
		this.day.setText(day);
		
		this.hour.setText(hour);
		this.minute.setText(minute);
		this.ampm.setText(ampm);
		
		this.shockScheduled = shockScheduled;
		if(shockScheduled) {
			btnAddRemoveUpdate.setText("Remove");
		} else {
			btnAddRemoveUpdate.setText("Add");
		}
	}
	
	private void toggleAmPm() {
		if(ampm.getText().equals("AM")) {
			ampm.setText("PM");
		} else {
			ampm.setText("AM");
		}
	}
}
